﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Services.Exceptions;

namespace HiTeachers.Services
{
    public class ContactedRequestServices
    {
        //private static readonly IContactedRequestDao CtdRequestDao = NHibernateDaoFactory.Instance.GetDao<IContactedRequestDao>("contactedRequest");
        private static readonly IContactedRequestDao CtdRequestDao = NHibernateDaoFactory.Instance.GetContactedRequestDao();

        public static ContactedRequest GetContactedRequest(Guid ctdReqId)
        {
            return CtdRequestDao.GetById(ctdReqId, false);
        }

        public static ContactedRequest AddContactedRequest(ContactedRequest ctdRequest)
        {
            if (IsAlreadyContacted(ctdRequest.Teacher.ID, ctdRequest.Request.ID))
                throw new ObjectAlreadyExistException("Already contacted to this request!");

            return CtdRequestDao.Save(ctdRequest);
        }

        public static IList<ContactedRequest> GetContactedRequests(Guid teacherId)
        {
            return CtdRequestDao.GetContactedRequests(teacherId);
        }

        public static IList<Teacher> GetContactedTeachers(Guid requestId)
        {
            return CtdRequestDao.GetContactedTeachers(requestId);
        }

        public static bool IsAlreadyContacted(Guid teacherId, Guid requestId)
        {
            return CtdRequestDao.GetContactedRequest(teacherId, requestId) != null;
        }

        public static void DeleteContactedRequest(ContactedRequest ctdRequest)
        {
            ctdRequest.IsDeleted = true;
            CtdRequestDao.SaveOrUpdate(ctdRequest);
        }
    }
}
